package com.lrkj.payfee.service.impl;

import com.lrkj.common.constant.FeeConstants;
import com.lrkj.common.exception.BaseException;
import com.lrkj.common.exception.CustomException;
import com.lrkj.common.utils.DateUtils;
import com.lrkj.common.utils.LrkjDateUtil;
import com.lrkj.common.utils.NumberArithmeticUtils;
import com.lrkj.common.utils.StringUtils;
import com.lrkj.framework.utils.NumberUtils;
import com.lrkj.framework.utils.SerialNumUtil;
import com.lrkj.payfee.domain.*;
import com.lrkj.payfee.mapper.ReceiptMapper;
import com.lrkj.payfee.mapper.RuleCostMapper;
import com.lrkj.payfee.service.*;
import com.lrkj.waitress.domain.CruiserTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 收款单Service业务层处理
 *
 * @author yajun_ren
 * @date 2021-08-06
 */
@Service
public class ReceiptServiceImpl implements IReceiptService {
    @Autowired
    private ReceiptMapper receiptMapper;

    @Autowired
    private RuleCostMapper ruleCostMapper;

    @Autowired
    private IRoomNormService roomNormService;

    @Autowired
    private IReceiptRecordService receiptRecordService;

    @Autowired
    private IAdvanceReceiptService advanceReceiptService;

    @Autowired
    private IAgainstRecordService againstRecordService;

    /**
     * 查询收款单
     *
     * @param receiptId 收款单ID
     * @return 收款单
     */
    @Override
    public Receipt selectReceiptById(String receiptId) {
        return receiptMapper.selectReceiptById(receiptId);
    }

    /**
     * 查询收款单列表
     *
     * @param receipt 收款单
     * @return 收款单
     */
    @Override
    public List<Receipt> selectReceiptList(Receipt receipt) {
        return receiptMapper.selectReceiptList(receipt);
    }

    /**
     * 新增收款单
     *
     * @param receipt 收款单
     * @return 结果
     */
    @Override
    @Transactional
    public int insertReceipt(Receipt receipt) {
        return receiptMapper.insertReceipt(receipt);
    }


    /**
     * 修改收款单
     *
     * @param receipt 收款单
     * @return 结果
     */
    @Override
    public int updateReceipt(Receipt receipt) {
        receipt.setUpdateTime(DateUtils.getNowDate());
        return receiptMapper.updateReceipt(receipt);
    }

    public String getNo(String key) {
        int nextVal = SerialNumUtil.getInstance().nextInt(key);
        return key + NumberUtils.frontCompWithZore(nextVal, 7);
    }

    /**
     * 批量删除收款单
     *
     * @param receiptIds 需要删除的收款单ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteReceiptByIds(String[] receiptIds) {

 
        return 1;
    }

    /**
     * 删除收款单信息
     *
     * @param receiptId 收款单ID
     * @return 结果
     */
    @Override
    public int deleteReceiptById(String receiptId) {
        return receiptMapper.deleteReceiptById(receiptId);
    }

    /**
     * 收款单作废
     *
     * @param receiptIds 收款单ID
     * @return 结果
     */
    @Override
    @Transactional
    public int nullify(String[] receiptIds) {

        return 1;
    }

    @Override
    public BigDecimal indexShow(String time) {
        return receiptMapper.indexReceivableMoney(time);
    }

    //检查当前作废收款单,是不是最新的一条
    private boolean checkBillDate(Receipt receipt) {
        Receipt param = new Receipt();
        param.setOwnerId(receipt.getOwnerId());
        param.setBillDate(receipt.getBillDate());
        return receiptMapper.checkBillDate(param) > 0 ? false : true;
    }

    /***
     * 按照冲抵记录进行回退  预收
     *
     * **/
    private void callbackAgainsRecord(Receipt receipt) {

    }


    //收款单作废  更新房屋收费标准的交乞年月
    private void updateRoomNormDeliveryTime(List<RuleCost> ruleCosts, Map<String, RoomNorm> roomNormMap) {

    }


    /**
     * 批量新增收款单
     *
     * @param receipts 收款单对象
     *
     */
    public int batchAddData(List<Receipt> receipts)
    {
        return receiptMapper.batchAddData(receipts);
    }
}
